package com.gperp.controller;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.druid.util.StringUtils;
import com.gperp.pojo.AsLogs;
import com.gperp.pojo.AsUser;
import com.gperp.service.AsUserService;
import com.gperp.service.LogsService;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;


@Aspect
public class LogAspect {

	@Autowired
    private HttpServletRequest request;
    @Autowired
    private LogsService logService;
    @Autowired
    private AsUserService userService;

    /**
     * 管理员登录方法的切入点
     */
    @Pointcut("execution(* com.gperp.service.impl.*.getUserLogin(..))")
    public void loginCell(){}

    @Pointcut("execution(* com.gperp.service.impl.*.save*(..))")
    public void saveCell(){}
    @Pointcut("execution(* com.gperp.service.impl.*.insert*(..))")
    public void insertCell(){}
    
    @Pointcut("execution(* com.gperp.service.impl.*.update*(..))")
    public void updateCell(){}
    
    @Pointcut("execution(* com.gperp.service.impl.*.delete*(..))")
    public void deleteCell(){}

    /**
     * 登录操作(后置通知)
     */
    @AfterReturning(value = "loginCell()", argNames = "joinPoint,object", returning = "object")
    public void loginLog(JoinPoint joinPoint, Object object) throws Throwable {
    	AsUser user=(AsUser)object;
        if (user == null || joinPoint.getArgs() == null){
        	return;
        }
        AsLogs log = new AsLogs();
        log.setOperateinfo("用户"+user.getUsername()+"登录");
        log.setUserid(user.getId());
        log.setUsername(user.getUsercode());
        log.setOperatedatetime(new Date());
        logService.savLog(log);
    }

    //添加操作
    @AfterReturning(value = "insertCell()", argNames = "joinPoint,object", returning = "object")
    public void insertLog(JoinPoint joinPoint, Object object) throws Throwable {
    	log("添加",joinPoint,object);
    }
    
    @AfterReturning(value = "saveCell()", argNames = "joinPoint,object", returning = "object")
    public void saveLog(JoinPoint joinPoint, Object object) throws Throwable {
    	log("添加",joinPoint,object);
    }
    
    //删除操作
    @AfterReturning(value = "deleteCell()", argNames = "joinPoint,object", returning = "object")
    public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable {
    	log("删除",joinPoint,object);
    }
    
    //修改操作
    @AfterReturning(value = "updateCell()", argNames = "joinPoint,object", returning = "object")
    public void updateLog(JoinPoint joinPoint, Object object) throws Throwable {
    	log("修改",joinPoint,object);
    }
    
    public void log(String type, JoinPoint joinPoint, Object object){
    	AsUser user = getUser();
        if (joinPoint.getArgs() == null||user==null) {
            return;
        }
        AsLogs log = new AsLogs();
        log.setOperateinfo(type+getTypeName(joinPoint.getSignature().getName())+"数据成功");
        log.setUserid(user.getId());
        log.setUsername(user.getUsercode());
        log.setOperatedatetime(new Date());
        logService.savLog(log);
    }
    
    private String getTypeName(String methodName){
    	methodName = org.apache.commons.lang3.StringUtils.lowerCase(methodName);
    	System.out.println("============"+methodName);
    	String type = "";
    	if(methodName.indexOf("user")>=0){
    		type = "用户";
    	}else if(methodName.indexOf("account")>=0){
    		type = "账号";
    	}else if(methodName.indexOf("accountdetail")>=0){
    		type = "账号明细";
    	}else if(methodName.indexOf("contacts")>=0){
    		type = "联系人";
    	}else if(methodName.indexOf("customs")>=0){
    		type = "客户";
    	}else if(methodName.indexOf("function")>=0){
    		type = "功能";
    	}else if(methodName.indexOf("keywords")>=0){
    		type = "关键词";
    	}else if(methodName.indexOf("role")>=0){
    		type = "角色";
    	}else if(methodName.indexOf("premission")>=0){
    		type = "权限";
    	}else if(methodName.indexOf("systemconfig")>=0){
    		type = "系统配置";
    	}
    	return type;
    }
    
    
    //获得当前用户
    public AsUser getUser(){
    	return userService.queryUserByID(Long.parseLong(request.getHeader("Authorization")));
    }
}
